﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

namespace UserInterfaceLayer.User
{
    public partial class Cost : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            lblFailure.Visible = false;
            lblInvalidInput.Visible = false;
            lblSuccessful.Visible = false;
            lblNewItem.Visible = false;
            lblDeletedItem.Visible = false;
            btnDelete.Visible = false;
        }

        protected void Page_PreRender(object sender, EventArgs e)
        {
            string strCostID = Request.QueryString["CostID"];
            if (strCostID != "add")
            {
                int CostID;
                if (!int.TryParse(strCostID, out CostID))
                    Response.Redirect("~/");
                DataAccessLayer.linqDatabaseDataContext db = new DataAccessLayer.linqDatabaseDataContext();
                DataAccessLayer.Cost Cost = DataAccessLayer.Cost.FindByCostID_IncludeDeleted(db, CostID);
                if (Cost == null)
                    Response.Redirect("~/");
                FromDatabase(Cost);
                btnDelete.Visible = true;
                lvHistory.DataSource = BusinessLogicLayer.DataObject.Cost.Convert(DataAccessLayer.Cost.HistoryToListByCostID(db, CostID));
                lvHistory.DataBind();
            }
        }

        protected void btnSave_Click(object sender, EventArgs e)
        {
            try
            {
                int UserID = (int)Session["UserID"];
                string strCostID = Request.QueryString["CostID"];
                string strRequestID = Request.QueryString["RequestID"];
                int Price;
                if (!int.TryParse(txtPrice.Text, out Price))
                {
                    lblInvalidInput.Visible = true;
                    return;
                }
                if (strCostID != "add")
                {
                    int CostID = int.Parse(strCostID);
                    DataAccessLayer.linqDatabaseDataContext db = new DataAccessLayer.linqDatabaseDataContext();
                    //DataAccessLayer.Cost Cost = DataAccessLayer.Cost.FindByCostID_IncludeDeleted(db, CostID);
                    DataAccessLayer.Cost NewCost = new DataAccessLayer.Cost();
                    NewCost.CostID = CostID;
                    NewCost.EditorUserID = UserID;
                    NewCost.Timestamp = DateTime.Now;
                    ToDatabase(NewCost);
                    db.Costs.InsertOnSubmit(NewCost);
                    db.SubmitChanges();
                    lblSuccessful.Visible = true;
                }
                else
                {
                    int RequestID = int.Parse(strRequestID);
                    DataAccessLayer.linqDatabaseDataContext db = new DataAccessLayer.linqDatabaseDataContext();
                    DataAccessLayer.sCost sCost = new DataAccessLayer.sCost();
                    sCost.RequestID = RequestID;
                    db.sCosts.InsertOnSubmit(sCost);
                    db.SubmitChanges();
                    DataAccessLayer.Cost NewCost = new DataAccessLayer.Cost();
                    NewCost.CostID = sCost.ID;
                    NewCost.EditorUserID = UserID;
                    NewCost.Timestamp = DateTime.Now;
                    ToDatabase(NewCost);
                    db.Costs.InsertOnSubmit(NewCost);
                    db.SubmitChanges();
                    lblNewItem.Visible = true;
                    btnSave.Visible = false;
                }
            }
            catch (Exception)
            {
                lblFailure.Visible = true;
            }
        }

        protected void btnDelete_Click(object sender, EventArgs e)
        {
            try
            {
                int UserID = (int)Session["UserID"];
                string strCostID = Request.QueryString["CostID"];
                int CostID = int.Parse(strCostID);
                int Price;
                if (!int.TryParse(txtPrice.Text, out Price))
                {
                    lblInvalidInput.Visible = true;
                    return;
                }
                DataAccessLayer.linqDatabaseDataContext db = new DataAccessLayer.linqDatabaseDataContext();
                //DataAccessLayer.Cost Cost = DataAccessLayer.Cost.FindByCostID_IncludeDeleted(db, CostID);
                DataAccessLayer.Cost NewCost = new DataAccessLayer.Cost();
                NewCost.CostID = CostID;
                NewCost.EditorUserID = UserID;
                NewCost.Timestamp = DateTime.Now;
                NewCost.IsDeleted = true;
                ToDatabase(NewCost);
                db.Costs.InsertOnSubmit(NewCost);
                db.SubmitChanges();
                lblSuccessful.Visible = true;
            }
            catch (Exception)
            {
                lblFailure.Visible = true;
            }
        }

        public void FromDatabase(DataAccessLayer.Cost Cost)
        {
            txtDescription.Text = Cost.Description;
            txtPrice.Text = Cost.Price.ToString();
            lblDeletedItem.Visible = Cost.IsDeleted;
        }

        public void ToDatabase(DataAccessLayer.Cost Cost)
        {
            Cost.Description = txtDescription.Text;
            Cost.Price = int.Parse(txtPrice.Text);
        }
    }
}